{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python3\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 逻辑与数据\n",
    "samples_and = [\n",
    "[0, 0, 0],\n",
    "[1, 0, 0],\n",
    "[0, 1, 0],\n",
    "[1, 1, 1],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 逻辑或数据\n",
    "samples_or = [\n",
    "[0, 0, 0],\n",
    "[1, 0, 1],\n",
    "[0, 1, 1],\n",
    "[1, 1, 1],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#逻辑异或数据\n",
    "samples_xor = [\n",
    "[0, 0, 0],\n",
    "[1, 0, 1],\n",
    "[0, 1, 1],\n",
    "[1, 1, 0],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def perceptron(samples):\n",
    "    w = np.array([1, 2])\n",
    "    b = 0\n",
    "    a = 1\n",
    "\n",
    "    for i in range(10):\n",
    "        for j in range(4):\n",
    "            x = np.array(samples[j][:2])\n",
    "            y = 1 if np.dot(w, x) + b > 0 else 0\n",
    "            d = np.array(samples[j][2])\n",
    "\n",
    "            delta_b = a*(d-y)\n",
    "            delta_w = a*(d-y)*x\n",
    "\n",
    "            print('epoch {} sample {} [{} {} {} {} {} {} {}]'.format(\n",
    "                i, j, w[0], w[1], b, y, delta_w[0], delta_w[1], delta_b\n",
    "            ))\n",
    "\n",
    "            w = w + delta_w\n",
    "            b = b + delta_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical and\n",
      "epoch 0 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1 [1 2 0 1 -1 0 -1]\n",
      "epoch 0 sample 2 [0 2 -1 1 0 -1 -1]\n",
      "epoch 0 sample 3 [0 1 -2 0 1 1 1]\n",
      "epoch 1 sample 0 [1 2 -1 0 0 0 0]\n",
      "epoch 1 sample 1 [1 2 -1 0 0 0 0]\n",
      "epoch 1 sample 2 [1 2 -1 1 0 -1 -1]\n",
      "epoch 1 sample 3 [1 1 -2 0 1 1 1]\n",
      "epoch 2 sample 0 [2 2 -1 0 0 0 0]\n",
      "epoch 2 sample 1 [2 2 -1 1 -1 0 -1]\n",
      "epoch 2 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 2 sample 3 [1 2 -2 1 0 0 0]\n",
      "epoch 3 sample 0 [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 1 [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 3 [1 2 -2 1 0 0 0]\n",
      "epoch 4 sample 0 [1 2 -2 0 0 0 0]\n",
      "epoch 4 sample 1 [1 2 -2 0 0 0 0]\n",
      "epoch 4 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 4 sample 3 [1 2 -2 1 0 0 0]\n",
      "epoch 5 sample 0 [1 2 -2 0 0 0 0]\n",
      "epoch 5 sample 1 [1 2 -2 0 0 0 0]\n",
      "epoch 5 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 5 sample 3 [1 2 -2 1 0 0 0]\n",
      "epoch 6 sample 0 [1 2 -2 0 0 0 0]\n",
      "epoch 6 sample 1 [1 2 -2 0 0 0 0]\n",
      "epoch 6 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 6 sample 3 [1 2 -2 1 0 0 0]\n",
      "epoch 7 sample 0 [1 2 -2 0 0 0 0]\n",
      "epoch 7 sample 1 [1 2 -2 0 0 0 0]\n",
      "epoch 7 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 7 sample 3 [1 2 -2 1 0 0 0]\n",
      "epoch 8 sample 0 [1 2 -2 0 0 0 0]\n",
      "epoch 8 sample 1 [1 2 -2 0 0 0 0]\n",
      "epoch 8 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 8 sample 3 [1 2 -2 1 0 0 0]\n",
      "epoch 9 sample 0 [1 2 -2 0 0 0 0]\n",
      "epoch 9 sample 1 [1 2 -2 0 0 0 0]\n",
      "epoch 9 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 9 sample 3 [1 2 -2 1 0 0 0]\n",
      "logical or\n",
      "epoch 0 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 1 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 2 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 3 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 4 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 4 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 4 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 4 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 5 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 5 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 5 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 5 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 6 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 6 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 6 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 6 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 7 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 7 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 7 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 7 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 8 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 8 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 8 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 8 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 9 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 9 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 9 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 9 sample 3 [1 2 0 1 0 0 0]\n",
      "logical xor\n",
      "epoch 0 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 3 [1 2 0 1 -1 -1 -1]\n",
      "epoch 1 sample 0 [0 1 -1 0 0 0 0]\n",
      "epoch 1 sample 1 [0 1 -1 0 1 0 1]\n",
      "epoch 1 sample 2 [1 1 0 1 0 0 0]\n",
      "epoch 1 sample 3 [1 1 0 1 -1 -1 -1]\n",
      "epoch 2 sample 0 [0 0 -1 0 0 0 0]\n",
      "epoch 2 sample 1 [0 0 -1 0 1 0 1]\n",
      "epoch 2 sample 2 [1 0 0 0 0 1 1]\n",
      "epoch 2 sample 3 [1 1 1 1 -1 -1 -1]\n",
      "epoch 3 sample 0 [0 0 0 0 0 0 0]\n",
      "epoch 3 sample 1 [0 0 0 0 1 0 1]\n",
      "epoch 3 sample 2 [1 0 1 1 0 0 0]\n",
      "epoch 3 sample 3 [1 0 1 1 -1 -1 -1]\n",
      "epoch 4 sample 0 [0 -1 0 0 0 0 0]\n",
      "epoch 4 sample 1 [0 -1 0 0 1 0 1]\n",
      "epoch 4 sample 2 [1 -1 1 0 0 1 1]\n",
      "epoch 4 sample 3 [1 0 2 1 -1 -1 -1]\n",
      "epoch 5 sample 0 [0 -1 1 1 0 0 -1]\n",
      "epoch 5 sample 1 [0 -1 0 0 1 0 1]\n",
      "epoch 5 sample 2 [1 -1 1 0 0 1 1]\n",
      "epoch 5 sample 3 [1 0 2 1 -1 -1 -1]\n",
      "epoch 6 sample 0 [0 -1 1 1 0 0 -1]\n",
      "epoch 6 sample 1 [0 -1 0 0 1 0 1]\n",
      "epoch 6 sample 2 [1 -1 1 0 0 1 1]\n",
      "epoch 6 sample 3 [1 0 2 1 -1 -1 -1]\n",
      "epoch 7 sample 0 [0 -1 1 1 0 0 -1]\n",
      "epoch 7 sample 1 [0 -1 0 0 1 0 1]\n",
      "epoch 7 sample 2 [1 -1 1 0 0 1 1]\n",
      "epoch 7 sample 3 [1 0 2 1 -1 -1 -1]\n",
      "epoch 8 sample 0 [0 -1 1 1 0 0 -1]\n",
      "epoch 8 sample 1 [0 -1 0 0 1 0 1]\n",
      "epoch 8 sample 2 [1 -1 1 0 0 1 1]\n",
      "epoch 8 sample 3 [1 0 2 1 -1 -1 -1]\n",
      "epoch 9 sample 0 [0 -1 1 1 0 0 -1]\n",
      "epoch 9 sample 1 [0 -1 0 0 1 0 1]\n",
      "epoch 9 sample 2 [1 -1 1 0 0 1 1]\n",
      "epoch 9 sample 3 [1 0 2 1 -1 -1 -1]\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    print('logical and')\n",
    "    perceptron(samples_and)\n",
    "    print('logical or')\n",
    "    perceptron(samples_or)\n",
    "    print('logical xor')\n",
    "    perceptron(samples_xor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
